<?php
declare (strict_types=1);

namespace app\command;

use app\home\model\User;
use app\home\model\UserCoupon;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * Class Coupon
 * 修改 失效优惠券
 */
class Randomcode extends Command{

    protected function configure()
    {
        $this->setName('randomcode')->setDescription('randomcode');
    }

    /**
     * execute()方法 就是 运行该命令行类的时候要执行的具体业务逻辑代码
     */
    protected function execute(Input $input, Output $output)
    {
        $user = new User();
        $userList = $user->select()->toArray();
        foreach ($userList as $key => $value){
            $str = $this->rand_str(12);
            $user->where(['id' =>$value['id']])->update(['random_code' => $str]);
        }
    }

    public function rand_str($number){
        $str = '';
        for ($i=0;$i < $number;$i++){
            $str .=  mt_rand(0,9);

        }

        $user = new User();
        $userInfo = $user->where(['random_code' => $str])->find();
        if ($userInfo){
            $this->rand_str();
        }
        return  $str;
    }

}